package com.dianqu.weather.core.common;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

public class AsyncAdvice implements MethodBeforeAdvice, MethodInterceptor,
		AfterReturningAdvice {

	SimpleAsyncTaskExecutor simpleAsyncTaskExecutor;

	public void afterReturning(Object returnValue, Method method,
			Object[] args, Object target) throws Throwable {
	}

	public void before(Method method, Object[] args, Object target)
			throws Throwable {
	}

	public Object invoke(final MethodInvocation invocation) throws Throwable {
		simpleAsyncTaskExecutor.execute(new Runnable() {
			public void run() {
				try {
					invocation.proceed();
				} catch (Throwable e) {
					e.printStackTrace();
				}
			}
		});
		return null;
	}

	public void setSimpleAsyncTaskExecutor(
			SimpleAsyncTaskExecutor simpleAsyncTaskExecutor) {
		this.simpleAsyncTaskExecutor = simpleAsyncTaskExecutor;
	}

}
